package com.zzxx.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomerAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setFlag(true);
        //authentication.getPrincipal()就是一个User,存放了登录成功的信息
        resultInfo.setData(authentication.getPrincipal());
        objectMapper.writeValue(response.getWriter(),resultInfo);
    }
}
